home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / e / lsthndln.lha / listhandling.e < prev    next >
Text File  |  1995-11-09  |  4KB  |  135 lines

  1. /* This module contain linked-list support stuff. */
  2. /* By Eric Sauvageau */
  3.  
  4. OPT MODULE
  5.  
  6. MODULE 'exec/nodes', 'exec/lists'
  7.  
  8.  
  9. -> +--------------------------------+
  10. -> | Initialize an Exec linked list.|
  11. -> +--------------------------------+
  12. EXPORT PROC lh_newList()
  13. DEF l:PTR TO mlh
  14.   l:=NewR(SIZEOF mlh)
  15.   l.head:=l+4
  16.   l.tail:=NIL
  17.   l.tailpred:=l
  18. ENDPROC l
  19.  
  20.  
  21. -> +------------------------------------------------+
  22. -> | Create a new node of size "size" named "name". |
  23. -> +------------------------------------------------+
  24. EXPORT PROC lh_newnode(name,size)
  25. DEF l:PTR TO ln
  26.   l:=NewR(size)
  27.   l.name:=String(StrLen(name))
  28.   StrCopy(l.name,name,ALL)
  29. ENDPROC l
  30.  
  31.  
  32. ->  ------------------------------------------------------
  33. ->  Add a node to a given list, keeping the list sorted.
  34. ->  Returns position inserted, or -1 if an error occured.
  35. ->  ------------------------------------------------------
  36.  
  37. EXPORT PROC lh_addNodeSorted(list:PTR TO mlh,node:PTR TO ln)
  38. DEF compareResult, done=FALSE, current_node:PTR TO ln, position = 0
  39.  
  40.   IF node = NIL THEN RETURN -1
  41.   current_node:=list.head
  42.   IF list.tailpred=list
  43.      AddHead(list, node)
  44.   ELSEIF (compareResult:=OstrCmp(current_node.name,node.name)) <> 0
  45.      IF compareResult = -1
  46.        AddHead(list, node)
  47.      ELSEIF current_node=list.tailpred
  48.        AddTail(list, node)
  49.      ELSE
  50.        WHILE done=FALSE
  51.          current_node:=current_node.succ
  52.          position := position +1
  53.          IF current_node.succ=NIL
  54.            done:=TRUE
  55.          ELSEIF (compareResult:=OstrCmp(current_node.name,node.name)) <= 0
  56.            IF compareResult=0 THEN done:=10 ELSE done := TRUE
  57.          ENDIF
  58.        ENDWHILE
  59.        IF (done <> 10)
  60.           Insert(list, node, current_node.pred)
  61.        ELSE
  62.           position := -1
  63.        ENDIF
  64.      ENDIF
  65.   ENDIF
  66. ENDPROC position
  67.  
  68.  
  69. ->  +-----------------------------------------------+
  70. ->  | Free all memory for this list                 |
  71. ->  | Must be a standard list with "ln" nodes!      |
  72. ->  +-----------------------------------------------+
  73.  
  74. EXPORT PROC lh_freeList(list:PTR TO mlh,deallocate=FALSE)
  75. DEF node:PTR TO ln
  76.     IF list = NIL THEN RETURN     /* already de-allocated */
  77.  
  78.     REPEAT
  79.        node := RemHead(list)
  80.        IF node <> 0
  81.           DisposeLink(node.name)
  82.           Dispose(node)
  83.        ENDIF
  84.     UNTIL node = 0
  85.     IF deallocate = TRUE THEN Dispose(list)
  86. ENDPROC
  87.  
  88.  
  89. ->  +----------------------------------------------+
  90. ->  | Returns the position of node "name", or -1.  |
  91. ->  +----------------------------------------------+
  92.  
  93. EXPORT PROC lh_getPosition(list:PTR TO mlh,name)
  94. DEF position=0, node: PTR TO ln
  95.  
  96.   IF list.tailpred = list THEN RETURN (-1)
  97.  
  98.   node:=list.head
  99.   REPEAT
  100.      IF StrCmp(node.name,name) THEN RETURN position
  101.      node:=node.succ
  102.      position := position +1
  103.   UNTIL (node.succ = NIL)
  104. ENDPROC (-1)
  105.  
  106.  
  107. ->  +---------------------------------------+
  108. ->  | Inputs a position number and a list.  |
  109. ->  | Return a PTR to the wanted node.      |
  110. ->  +---------------------------------------+
  111. EXPORT PROC lh_getNode(list:PTR TO mlh,position)
  112. DEF node: PTR TO ln,i
  113.   node:=list.head
  114.   FOR i:=1 TO position DO node:=node.succ
  115. ENDPROC (node)
  116.  
  117.  
  118. ->  +----------------------------------------------+
  119. ->  | Returns the number of node linked to "list". |
  120. ->  +----------------------------------------------+
  121.  
  122. EXPORT PROC lh_itemsTotal(list:PTR TO mlh)
  123. DEF items=0, node:PTR TO ln
  124.  
  125.   IF list.tailpred=list THEN RETURN 0
  126.   node := list.head
  127.   REPEAT
  128.      items := items +1
  129.      node:=node.succ
  130.   UNTIL node.succ = NIL
  131. ENDPROC items
  132.  
  133. CHAR 'listhandling.m 1.0 (9.11.95)',0
  134.  
  135.